import React, { useState } from 'react'
import { getCurrentInstance } from '@tarojs/taro'
import { useEffect } from 'react';
import Offline from './components/offline';
import { backgroundObj, setSessionItem } from '@/utils/utils';
import Router from '@/utils/router';
import { fetchViewGoods, fetchGoodsDetail } from '@/server/shop'
import { loginData } from '@/utils/utils'
import './index.scss'
const shopDetail = (props) => {
  const { params } = getCurrentInstance().router
  const { goodsId = '', linkType, openType } = params
  const [data, setData] = useState({})
  const { organizationId = '1', fromGoodsId, fromGoodsType, unionId } = loginData || {}
  const { goodsBaseInfo = {} } = data
  const { goodsType = 'offline' } = goodsBaseInfo
  const fetchData = () => {
    fetchGoodsDetail({
      organizationId, goodsId, fromGoodsId, fromGoodsType
    }).then(e => {
      const { goodsBaseInfo = {}, userGoodsOwnerInfo = {}, secKillSkuList, directLiveInfo = {}, liveInfo = {} } = e
      const { priceType, goodsType, id } = goodsBaseInfo
      const { ownerFlag, orderSn, orderFlag } = userGoodsOwnerInfo
      setSessionItem({ goodsId: id, goodsType })
      fakeViewGoods(goodsType)
      const data = e
      if (secKillSkuList) {
        data.goodsSkuList = secKillSkuList.map(item => {
          return {
            ...item,
            oriPrice: item.price,
            price: item.secKillPrice,
            stock: item.secKillStock,
            id: item.skuId
          }
        })
      }
      if (goodsType === 'live' || goodsType === 'directLive') {
        if ((priceType === 'free' || ownerFlag === '1') && linkType !== 'pay') {
          if (goodsType === 'liveCourse') {
            Router('payLiveInteraction', {
              args: {
                goodsId: id,
                goodsType,
              },
              linkType: 'redirectTo'
            })

          } else if (goodsType === 'directLive') {
            const { liveStatus } = directLiveInfo
            if (liveStatus === '2') {
              Router('payLiveInteraction', {
                args: {
                  goodsId: id,
                  goodsType,
                },
                linkType: 'redirectTo'
              })
            }
            else {
              Router('liveInteraction', {
                args: {
                  goodsId: id,
                  goodsType,
                  openType
                },
                linkType: 'redirectTo'
              })
            }

          } else if (goodsType === 'live') {
            const { liveStatus } = liveInfo
            if (liveStatus === '2') {
              Router('payLiveInteraction', {
                args: {
                  goodsId: id,
                  goodsType,
                },
                linkType: 'redirectTo'
              })
            }
            else {
              Router('liveInteraction', {
                args: {
                  goodsId: id,
                  goodsType,
                  openType
                },
                linkType: 'redirectTo'
              })
            }

          }
        } else if (orderFlag === '1') {
          setData(data)
          Router('orderWeex', {
            args: {
              orderSn,
            },
          })
        }
        else {
          setData(data)
        }
      }
      else {
        if (ownerFlag === '1') {
          Router('payShopDetail', {
            args: {
              goodsId: id,
              goodsType,
            },
            linkType: 'redirectTo'
          })
        }
        else {
          setData(data)
        }
      }
    })
  }
  useEffect(() => {
    fetchData()
  }, [])
  const renderObj = {
    offline: <Offline reload={fetchData} data={data}></Offline>,
    videoCourse: <Offline reload={fetchData} data={data}></Offline>,
    directLive: <Offline reload={fetchData} data={data}></Offline>,
    liveCourse: <Offline reload={fetchData} data={data}></Offline>,
    video: <Offline reload={fetchData} data={data}></Offline>,
    faceToFace: <Offline reload={fetchData} data={data}></Offline>,
    service: <Offline reload={fetchData} data={data}></Offline>,
  }[goodsType]

  console.log(goodsType, '333')
  const fakeViewGoods = (goodsType) => {
    fetchViewGoods({ organizationId, goodsId, fromGoodsType, fromGoodsId, unionId, goodsType })
  }
  if (Object.keys(data).length > 0) {

    return (
      renderObj
    )
  }
  console.log(111)
  return null

}
export default shopDetail
